﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;

using BartonExtension;
using Cache;
namespace WSServer
{
    public class WSConnectionIndex
    {
        public string identifier { get; set; }
    }
    public class WSConnectionIndexer
    {
        const string INDEX_PATH_ID = "connIndex";
        const string INDEX_FILE_NAME = "connections.index";
        public static bool IndexConnections(List<WSSocket> connections)
        {
/*string path = HttpRuntime.AppDomainAppPath + INDEX_PATH_ID;
            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string file = path + "\\" + INDEX_FILE_NAME;
            if(FileIO.isFileCanUse(file) || !File.Exists(file))
            {
                List<WSConnectionIndex> _indexes = indexes;
                using(FileStream fs = new FileStream(file,FileMode.OpenOrCreate,FileAccess.Write))
                {
                    connections.ForEach(c =>
                    {
                        int index = _indexes.FindIndex(i =>
                        {
                            return i.identifier == c.identifier;
                        });
                        if(index == -1)
                        {
                            _indexes.Add(new WSConnectionIndex()
                            {
                                identifier = c.identifier
                            });
                        }
                    });
                    byte[] buffer = Encoding.UTF8.GetBytes(_indexes.ToJson());
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Close();
                }
                return true;
            }
            else
            {
                return false;
            }*/
            List<WSConnectionIndex> _indexes = indexes;
            connections.ForEach(c =>
            {
                int index = _indexes.FindIndex(i =>
                {
                    return i.identifier == c.identifier;
                });
                if (index == -1)
                {
                   // _indexes.Add(new WSConnectionIndex()
                    //{
                        //identifier = c.identifier
                    _indexes.Add(new WSConnectionIndex()
                    {
                        identifier = c.identifier
                    });
                    int size = Cache.CacheManager.instance.doCacheObject(connections, c.identifier).retryCount;
                    //});
                }
            });
            return true;
           

        }
        public static bool isConnectionIdExists(string connectionId)
        {
            List<WSConnectionIndex> _idxes = indexes;
            if(!_idxes.isNullCollectionOrCount())
            {
                int index = _idxes.FindIndex(i =>
                {
                    return i.identifier == connectionId;
                });
                return index != -1;
            }
            return false;
        }

        public static List<WSConnectionIndex> indexes
        {
            get
            {
                List<WSConnectionIndex> _indexes = new List<WSConnectionIndex>();
                var filePath = HttpRuntime.AppDomainAppPath + INDEX_PATH_ID + "\\" + INDEX_FILE_NAME;
                if(File.Exists(filePath))
                {
                    if(FileIO.isFileCanUse(filePath))
                    {
                        try
                        {
                            string jsonStr = File.ReadAllText(filePath, Encoding.UTF8);
                            _indexes = jsonStr.FromJson<List<WSConnectionIndex>>();
                        }
                        catch (Exception e)
                        {
                            _indexes = new List<WSConnectionIndex>();
                        }
                    }
                }
                return _indexes;
            }
        }
    }
}
